/**
 * This function will retrieve the index of column from @columnName
 * @param grid JQGrid
 * @param columnName String
 */
function getColumnIndexByName(grid,columnName)
{
	  var cm = grid.jqGrid('getGridParam','colModel');
	  for (var i=0,l=cm.length; i<l; i++) {
	      if (cm[i].name===columnName) {
	          return i; // return the index
	      }
	  }
	  return -1;
}

/**
 * This is wrapper function of getRowData in JQGrid, used for specific @gridId 
 * @param gridId String
 * @param rowId 
 * @returns
 */
function getGridData(gridId,rowId)
{
	return $("#"+gridId).jqGrid('getRowData',rowId);
}

/**
 * This function add an button with id is @buttonId , title is @title
 * The @callfunction is the name of the function which executes when user click on the button
 * @param buttonId String
 * @param title String
 * @param callfunction String
 */
function addCustomButton(buttonId, title,buttonIcon, callfunction)
{
	$(jQGridId).jqGrid('navButtonAdd', pagerId, {
        caption: "",
	 	title:title,
        buttonicon: buttonIcon,
        id: buttonId,
        onClickButton: function () {
        	//Create the function call from function name and parameter.
//        	var funcCall = callfunction + "();";            	 
        	//Call the function
        	var ret = eval(callfunction);
        }
    });
}

/**
 * This function will change the style of the buttonId to disable
 * @param buttonId
 */
function disableCustomButton(buttonId)
{
	$("#"+buttonId).addClass('ui-state-disabled');
}

/**
 * This function will remove the disable style out of the buttonId
 * @param buttonId
 */
function enableCustomButton(buttonId)
{
	$("#"+buttonId).removeClass('ui-state-disabled');
}

/**
 * This function shows the saving message at the center of the grid and set grid active flag to false 
 */
function showSavingMessage(gridId)
{
	$("#load_"+gridId).html('<font color="#2E6E9E"> Saving ...</font>');
	$("#refresh_"+gridId).addClass('ui-state-disabled');
	$(".loading").css("display", "block");	
	activeGrid = false;
}

/**
 * This function hides the saving message at the center of the grid and set grid active flag to true 
 */
function hideSavingMessage(gridId)
{
	$("#load_"+gridId).html('<font color="#2E6E9E"> Loading ...</font>');
	$("#refresh_"+gridId).removeClass('ui-state-disabled');
	$(".loading").css("display", "none");	
	activeGrid = true;
}

/**
 * This function add new row with @data value to @gridId at last position
 * @param gridId
 * @param data
 */
function addNewRow(gridId, data, rowId)
{
    var su=jQuery("#"+gridId).addRowData(rowId,data,"last");
    if(su) 
    { 
    	jQuery("#"+gridId).jqGrid('editRow',rowId);
    };
};

/**
 * This is the utility function which converts grid data to JSON
 * @returns {String}
 */
function parseData(gridId,listRowID)
{
	var result = '{';
	for (var i=0;i<listRowID.length-1; ++i)
		{
			var data2 = JSON.stringify(getGridData(gridId,listRowID[i]), null, 2);
			result = result + i + ':' + data2+ ',';
		}
	var data2 = JSON.stringify(getGridData(gridId,listRowID[listRowID.length-1]), null, 2);
	result = result + (listRowID.length-1) + ':' + data2+ '}';
	return result;
}

/**
 * Change CSS Style of the invalid row
 */
function markAsInvalid(inValidRowId)
{
	for (var i=0; i<inValidRowId.length;++i)
		{
			markInvalidRow(inValidRowId[i]);
		}
}

function unMarkAsInvalid(inValidRowId)
{
	for (var i=0; i<inValidRowId.length;++i)
	{
		markValidRow(inValidRowId[i]);
	}
}

function markInvalidRow(rowId)
{
	$('#'+rowId).find("td").addClass('error-row');
}

function markInvalidRow(rowId)
{
	$('#'+rowId).find("td").addClass('error-row');
}

function markValidRow(rowId)
{
	$('#'+rowId).find("td").removeClass('error-row');
}

/**
 * Check if @n is number or not 
 * @param n
 * @returns {Boolean}
 */
function isNumber(n) {
	  return !isNaN(parseFloat(n)) && isFinite(n);
	}

/**
 * This is the utility function which removes @rowId out of @list
 * @param rowId
 */

function removeOutOfList(rowId,list)
{
	for (var i=0; i<list.length; ++i)
	{
		if (list[i] === rowId)
			{
				list.splice(i, 1);
			}
	}
	return list;
}
